package com.example.firstapp.utils;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

public class DatabaseHelper extends SQLiteOpenHelper {

    // 构造方法
    public DatabaseHelper(@Nullable Context context) {
        super(context, "db_sqlite.db", null, 1);
    }

    // 数据库只在第一次创建的时候调用该方法
    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d("TAG", "创建数据库: ");
        // 1.创建字段
        // String sql = "create table " + TABLE_NAME + "(_id integer primary key,name varchar(255),age integer,salary integer)";
        String sql = "create table " + Constants.TABLE_NAME + "(id integer primary key autoincrement,name varchar(64),age integer,email varchar(128))";
        // 2.执行sql语句
        db.execSQL(sql);
    }

    /**
     * 升级数据库版本的时候调用
     * @param db The database.
     * @param oldVersion The old database version.
     * @param newVersion The new database version.
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.d("TAG", "更新数据库: ");
    }
}
